const fp = require('lodash/fp')
const { Maybe, Container } = require('./support')
let maybe = Maybe.of([5,6,1])

// 练习1 － 给Funtor中数组的每个值+3
let ex1 = (x) => {
	let f1 = fp.map(fp.add(x))
	return maybe.map(f1)
}
console.log(ex1(3)._value)

// 练习2 － 获取第一个元素的值
let xs = Container.of(['do', 'ray', 'me', 'fa', 'so', 'la', 'ti', 'do'])
let ex2 = () => {
	return xs.map(fp.first)._value
}
console.log(ex2())

// 练习3 － 找到user名字的首字母
let safeProp = fp.curry(function(x, o) {
	return Maybe.of(o[x])
})
let user = { id: 2, name: 'Albert'}
let ex3 = (value) => {
	let name = safeProp('name', value)
	return fp.first(name._value.split(''))
}
console.log(ex3(user))

// 练习4 － 使用Maybe重写ex4，不要有if语句
// let ex4 = function (n) {
// 	if (n) {
// 		return parseInt(n)
// 	}
// }
let num = Maybe.of(12.2)
let ex4 = num.map(parseInt)
console.log(ex4)